/** 
* @file vmu_pte.cpp
* Page Table Entries (PTE).
* Provides abstraction interface to aid in managment of PTEs.
* Page Table Entry is what represent PAGE.
* Page is a fixed-length block of memory. This block of memory
* can reside in physical memor (or in harddrive - swaping).
*
* Copyrights 2010 Michal Saman, m.saman@designplus.cz.
* This source code is release under the Apache License 2.0.
* http://www.apache.org/licenses/LICENSE-2.0
*/

#include <mem/vmu_pte.h>
#include <main/kprintf.h>

/**
* Sets a flag in the Page Table Entry (page).
* @param e Pointer to PTE
* @param attrib Attribute to set - from PAGE_PTE_FLAGS
*/
inline void pte_add_attrib(pte *e, uint32_t attrib) {
	*e |= attrib;
};

/**
* Clears a flag in the Page Table Entry (page).
* @param e Pointer of PTE
* @param attrib Attribute to clear - from PAGE_PTE_FLAGS
*/
inline void pte_del_attrib(pte *e, uint32_t attrib) {
	*e &= ~attrib;
}

/**
* Set a frame address (addr of physical memory block) to Page Table Entry (page).
* Frame address is highest 20 bits of PTE - Bits 12-31 (FRAME).
* @param e Pointer of PTE
* @param addr physical_addr of memory block
*/
inline void pte_set_frame(pte *e, physical_addr addr) {
	*e = (*e & ~PTE_FRAME) | ((addr / PMU_BLOCK_SIZE) << 12);
	//kprintf("PTE: 0x%x, ~Mask: 0x%x, ~Mask AND PDE: 0x%x, frame addr: 0x%x\n", *e, ~PTE_FRAME, (*e & ~PTE_FRAME),(addr / PMU_BLOCK_SIZE));
}

bool pte_is_present(pte e) {
	return e & PTE_PRESENT;
};

bool pte_is_writable(pte e) {
	return e & PTE_WRITABLE;
}

physical_addr pte_get_frame(pte e) {
	return (e >> 12) * PMU_BLOCK_SIZE;
}

